package DecodeString;

import java.util.Stack;


public class Solution {
    public String decodeString(String s) {
    	s="1["+s+"]";
    	return (String) doDecode(s,2,1)[0];
    }

	private Object[] doDecode(String s, int head,int k) {
		StringBuilder sb=new StringBuilder();
		StringBuilder resultString=new StringBuilder();
		Object[] bean=new Object[2];
		int index=head;
		int tempK=0,tempHead=0;
		char ch=s.charAt(index);
		while (ch!=']'){
			if ((ch-'0'>=0) && (ch-'0'<=9))
				tempK=tempK*10+(ch-'0');
			else if (ch=='[') {
				tempHead=index+1;
				Object[] ob=doDecode(s,tempHead,tempK);
				index=(int) ob[1];
				tempK=0;
				sb.append(ob[0]);
			}
			else sb.append(s.charAt(index));
			ch=s.charAt(++index);
		}
		for (int i=1;i<=k;i++)
			resultString.append(sb);
		bean[0]=resultString.toString();
		bean[1]=index;
		return bean;
	}
	
	public static void main(String args[]) {
		String s="3[a2[c]]";
		Solution so=new Solution();
		System.out.println(so.decodeString(s));
	}
}
